# app/utils.py

import os
import zipfile
import pandas as pd

def extract_files(zip_file, output_dir="attachments"):
    with zipfile.ZipFile(zip_file, 'r') as zip_ref:
        zip_ref.extractall(output_dir)
    return zip_ref.namelist()  # 返回解压后的文件名列表

def load_email_data(file):
    try:
        df = pd.read_excel(file)
        email_list = df['Email'].tolist()
        file_names = df['File'].tolist()
        return email_list, file_names
    except KeyError as e:
        raise ValueError(f"Excel format error: {e}. Please check column names.")

def personalize_body(body_template, recipient_email):
    name = recipient_email.split('@')[0]
    return body_template.replace('[name]', name)
